package com.bjt.wms.sys.service;

import com.bjt.wms.sys.model.Material;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WenbinJi
 * <p>
 * 耗材业务处理
 */
public class MaterialService {

    /**
     * 查询所有耗材信息
     */
    public List<Material> list() {
        return Material.dao.find("select * from material");
    }

    public List<Record> getMaterialByIds(String ids) {
        List<Record> records = new ArrayList<Record>();
        String[] strs = ids.split(",");
        for (int i = 0; i < strs.length; i++) {
            int material_id = Integer.valueOf(strs[i].split("-")[0]);
            int count = Integer.valueOf(strs[i].split("-")[1]);

            Record material = Db.findFirst("select * from material where id=" + material_id);
            material.set("count", count);
            records.add(material);
        }

        return records;
    }

    public int getMaterialIdByRfid(String rfid) {
        Material material = Material.dao.findFirst("select id from material where rfid='" + rfid + "'");
        if (material != null)
            return material.getId();
        else
            return 0;
    }
}
